package com.niiwoo.civet.account.service.local;

import com.niiwoo.civet.account.dao.entity.AccountCompany;
import com.niiwoo.civet.account.dao.mapper.AccountCompanyMapperExt;
import com.niiwoo.civet.account.dto.request.CompanyAccountRequestDTO;
import com.niiwoo.tripod.base.component.PrivacyMasks;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

@Service
public class AccountCompanyService {

    @Autowired
    AccountCompanyMapperExt accountCompanyMapper;

    @Autowired
    private PrivacyMasks privacyMasks;

    public boolean update(AccountCompany company) {
        return accountCompanyMapper.updateByPrimaryKeySelective(company) > 0;
    }

    public boolean save(AccountCompany company) {
        return accountCompanyMapper.insertSelective(company) > 0;
    }

    public boolean resetBankCardNoAndBankCode(Long accountId){
        return accountCompanyMapper.resetBankCardNoAndBankCode(accountId)>0;
    }

    @Transactional
    public AccountCompany queryAccountCompanyByOrgId(String orgId){
        return accountCompanyMapper.selectByUserId(orgId);
    }

    @Transactional
    public AccountCompany queryAuditPassedAccountCompany(String userId,String orgId,Byte accountType){
        return accountCompanyMapper.selectAuditPassedAccountCompany(userId,orgId,accountType);
    }

    public AccountCompany queryAccountCompanyByBusinessLicense(String businessLicense) {
        return accountCompanyMapper.selectByBusinessLicense(businessLicense);
    }

    public AccountCompany queryAccountCompanyByOrgIdAndCompanyOrgId(String orgId, String companyOrgId) {
        return accountCompanyMapper.selectByOrgIdAndCompanyOrgId(orgId, companyOrgId);
    }

    public AccountCompany queryByOrgIdAndCompanyOrgIdWithAccountType(String orgId, String companyOrgId,Byte accountType) {
        return accountCompanyMapper.selectByOrgIdAndCompanyOrgIdWithAccounType(orgId, companyOrgId,accountType);
    }

    public AccountCompany queryAccountCompanyByOrgIdAndUserIdWithAccountType(String userId,String orgId,Byte accountType){
        return accountCompanyMapper.selectByOrgIdAndUserIdWithAccountType(userId,orgId,accountType);
    }

    @Transactional
    public boolean updateAccountCompany(CompanyAccountRequestDTO reqDto) {
        Assert.notNull(reqDto.getAccountNo(),"accountNo不能为空");
        AccountCompany exsitAccountCompany = accountCompanyMapper.selectByAccountNo(reqDto.getAccountNo());

        AccountCompany company=new AccountCompany();
        company.setAccountId(exsitAccountCompany.getAccountId());
        company.setCompanyName(reqDto.getCompanyName());
        company.setLegalPersonName(reqDto.getLegalPersonName());
        company.setLegalPersonIdCard(StringUtils.isNotEmpty(reqDto.getLegalPersonIdCard())?privacyMasks.encryptPrivacy(reqDto.getLegalPersonIdCard()):reqDto.getLegalPersonIdCard());
        company.setBankLicence(reqDto.getBankLicence());
        company.setContactName(reqDto.getContactName());
        company.setContactMobileNo(StringUtils.isNotEmpty(reqDto.getContactMobileNo())?privacyMasks.encryptPrivacy(reqDto.getContactMobileNo()):reqDto.getContactMobileNo());
        company.setAuditStatus(reqDto.getAuditStatus());
        company.setUpdateTime(reqDto.getUpdateTime());
        company.setUpdateMangerId(reqDto.getUpdateMangerId());
        company.setOrgNo(reqDto.getOrgNo());
        company.setBusinessLicense(reqDto.getBusinessLicense());
        company.setTaxNo(reqDto.getTaxNo());
        company.setUnifiedCode(reqDto.getUnifiedCode());
        company.setCreditCode(reqDto.getCreditCode());
        int flag=accountCompanyMapper.updateByPrimaryKeySelective(company);
        return flag>0;
    }

    @Transactional(isolation = Isolation.READ_COMMITTED)
    public AccountCompany queryByAccountNo(String accountNo){
        return accountCompanyMapper.selectByAccountNo(accountNo);
    }
}
